package com.qinggeng.project.system.service.impl;

import com.qinggeng.common.enums.CommonEnum;
import com.qinggeng.common.utils.DateUtils;
import com.qinggeng.project.api.utils.AMapUtils;
import com.qinggeng.project.system.domain.*;
import com.qinggeng.project.system.mapper.UserLocationMapper;
import com.qinggeng.project.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户位置Service业务层处理
 *
 * @author qinggeng
 * @date 2020-06-26
 */
@Service
public class UserLocationServiceImpl implements IUserLocationService
{
    @Autowired
    private UserLocationMapper userLocationMapper;

    @Autowired
    private IOrdersService ordersService;

    @Autowired
    private IOrdersCourierService ordersCourierService;
    @Autowired
    private IShopService shopService;

    @Autowired
    private IOrdersAddressService ordersAddressService;

    @Autowired
    private IOrdersPointService ordersPointService;
    /**
     * 查询用户位置
     *
     * @return 用户位置
     */
    @Override
    public UserLocation selectUserLocationById(Long ordersId)
    {
        Orders orders = ordersService.selectOrdersById(ordersId);
        OrdersCourier ordersCourier = ordersCourierService.selectOrdersCourierByOrdersId(ordersId);
        UserLocation userLocation = userLocationMapper.selectUserLocationById(ordersCourier.getCourierId());
        if (orders.getDistStatus().equals(CommonEnum.STATUS_20.getValue())){
            Shop shop = shopService.selectShopById(orders.getShopId());
            double distance = AMapUtils.getDistance(userLocation.getLat(), userLocation.getLng(), shop.getLat(), shop.getLng());
            userLocation.setDistance("正在配送，距离商家"+Double.valueOf(String.format("%.2f", distance / 1000 ))+"km");
        }
        if (orders.getDistStatus().equals(CommonEnum.STATUS_30.getValue())){
            if (orders.getDistStatus().equals(CommonEnum.STATUS_10.getValue())){
                OrdersAddress ordersAddress = ordersAddressService.selectOrdersAddressByOrdersId(ordersId);
                double distance = AMapUtils.getDistance(userLocation.getLat(), userLocation.getLng(), ordersAddress.getAddressLat(), ordersAddress.getAddressLng());
                userLocation.setDistance("正在配送，当前距你"+Double.valueOf(String.format("%.2f", distance / 1000 ))+"km");
            }
            if (orders.getDistStatus().equals(CommonEnum.STATUS_20.getValue())){
                OrdersPoint ordersPoint = ordersPointService.selectOrdersPointByOrdersId(ordersId);
                double distance = AMapUtils.getDistance(userLocation.getLat(), userLocation.getLng(), ordersPoint.getLat(), ordersPoint.getLng());
                userLocation.setDistance("正在配送，距离配送点"+Double.valueOf(String.format("%.2f", distance / 1000 ))+"km");
            }
        }
        return userLocation;
    }

    /**
     * 查询用户位置列表
     *
     * @param userLocation 用户位置
     * @return 用户位置
     */
    @Override
    public List<UserLocation> selectUserLocationList(UserLocation userLocation)
    {
        return userLocationMapper.selectUserLocationList(userLocation);
    }

    /**
     * 新增用户位置
     *
     * @param userLocation 用户位置
     * @return 结果
     */
    @Override
    public int insertUserLocation(UserLocation userLocation)
    {
        userLocation.setCreateTime(DateUtils.getNowDate());
        return userLocationMapper.insertUserLocation(userLocation);
    }

    /**
     * 修改用户位置
     *
     * @param userLocation 用户位置
     * @return 结果
     */
    @Override
    public int updateUserLocation(UserLocation userLocation)
    {
        return userLocationMapper.updateUserLocation(userLocation);
    }

    /**
     * 批量删除用户位置
     *
     * @param userIds 需要删除的用户位置ID
     * @return 结果
     */
    @Override
    public int deleteUserLocationByIds(Long[] userIds)
    {
        return userLocationMapper.deleteUserLocationByIds(userIds);
    }

    /**
     * 删除用户位置信息
     *
     * @param userId 用户位置ID
     * @return 结果
     */
    @Override
    public int deleteUserLocationById(Long userId)
    {
        return userLocationMapper.deleteUserLocationById(userId);
    }

}
